const express = require("express");
const router = express.Router();
const db = require("../mysql/index");
const chuang=require('./unils/exlx')



//后台获取用户信息
router.post('/user/data',(req,res)=>{
    const sqlUrl='select * from houtai_user where openid= ?'
  
    console.log(req.user);
  
    db.query(sqlUrl,[req.user.id],(err,ress)=>{
  
  if(err) res.send({
    code: 404,
    message: "数据库查询用户信息失败",
  })
  
  if(ress.length!==0){
    // 生成token
    delete ress[0].openid
    res.send({code:200,
    data:ress[0],
    message:'登录成功！'
  })
  }else{
    res.send({code:201,
    
      message:'用户名或密码错误'
    })
  }
    })
  })

// 获取模组品牌
router.post('/getmozu',(req,res)=>{
    const sqlurlt = "select * from brand where openid = ?;";

   db.query(sqlurlt,req.user.id,(err,ress)=>{
    
if(err) return res.send({code:500,
    message:'获取模组品牌失败！'
})

if(ress){
    res.send({
        code:200,
        data:ress,
        message:'获取模组品牌成功'
    })
}
   })

 
})
// 获取模组型号
router.post('/getmozuModel',(req,res)=>{
    console.log(req.body);

    const sqlurlt = "select * from brand_model where brand_id=? and openid = ?";
    db.query(sqlurlt,[req.body.id,req.user.id],(err,ress)=>{
        console.log(ress);
        if(err) return res.send({code:500,
            message:'获取模组型号失败！'
        })

        if(ress){

            res.send({code:200,
            data:ress,
            message:'获取模组型号成功！'})
        }
    })
})


// 获取控制卡品牌
router.post('/getcard',(req,res)=>{
    const sqlurlt = "select * from control_card where openid = ?";
    db.query(sqlurlt,req.user.id,(err,ress)=>{
        if(err) return res.send({code:500,
            message:'获取控制卡品牌失败！'
        })
        if(ress){
            res.send({
                code:200,
                data:ress,
                message:'获取控制卡品牌成功'
            })
        }
    })
})

// 获取控制卡型号
router.post('/getCardxing',(req,res)=>{
    const sqlurlt = "select * from control_model where brand_id=? and openid = ?";
db.query(sqlurlt,[req.body.id,req.user.id],(err,ress)=>{
   
    if(err) return res.send({code:500,
        message:'获取控制卡型号失败！'
    })

    if(ress){
        res.send({
            code:200,
            data:ress,
            message:'获取控制卡型号成功'
        })
    }

})
})

// 获取电源品牌
router.post('/dianyuan',(req,res)=>{
    const sqlurlt =
    "select * from power where openid = ?";
db.query(sqlurlt,req.user.id,(err,ress)=>{
   
    if(err) return res.send({code:500,
        message:'获取电源品牌失败！'
    })

    if(ress){
        res.send({
            code:200,
            data:ress,
            message:'获取电源品牌成功'
        })
    }

})
})

// 获取电源型号
router.post('/dianyuanXing',(req,res)=>{
    const sqlurlt = "select * from power_model where brand_id=? and openid = ?";

db.query(sqlurlt,[req.body.id,req.user.id],(err,ress)=>{
   
    if(err) return res.send({code:500,
        message:'获取电源型号失败！'
    })

    if(ress){
        res.send({
            code:200,
            data:ress,
            message:'获取电源型号成功'
        })
    }

})
})
// 后台注册接口
router.post("/houtaizhuce",(req,res)=>{

    const sqlUrls='select * from houtai_user where openid = ?'
    db.query(sqlUrls,[req.user.id],(err,cv)=>{
        if(err) res.send({
            code: 404,
            message: "数据库查询后台注册用户信息失败",
          })
          else{
           
            // console.log(req.user.id,req.body);
            if(cv.length!==0){
                res.send({code:200,
                    message:'您已经注册过了！'
                  })
                
            }else{
                const sqlUrl='select * from houtai_user where userName = ?'
                db.query(sqlUrl,[req.body.name],(err,ress)=>{
              if(err) res.send({
                code: 404,
                message: "数据库查询用户信息失败",
              })
              
              if(ress.length!==0){
            
              
                res.send({code:200,
                message:'用户已存在！'
              })
            
            //   不存在插入账号
              }else{
             
                const sqlInsert = 'INSERT INTO houtai_user (userName, userpass,openid) VALUES (?, ?, ?)';
            // 执行插入操作
            db.query(sqlInsert, [req.body.name, req.body.password,req.user.id], (errz, result) => {
                if (errz) {
                    res.send({
                        code: 404,
                        message: "数据库插入c信息失败",
                      })

                      console.log(errz);
                  return;
                }
            
                else{

                    console.log(result);
                    const sqlInsert= 'UPDATE user SET status = ? where openid =?'
                    db.query(sqlInsert, [2,req.user.id], (errz, results) => {
                        if (err) {
                            res.send({
                                code: 404,
                                message: "数据库插入信息失败",
                              })
                          return;
                        }else{

                         
                            res.send({
                                code: 200,
                                data:result,
                                message: "注册成功！",
                              })
                        }
                    })
                   
                }
              
            
              });
              }
                })
            }

    
               
          }
    })

    })
// 获取处理器品牌
router.post('/chuliqi',(req,res)=>{
    const sqlurlt =
      "select * from processor where openid = ?";

db.query(sqlurlt,req.user.id,(err,ress)=>{
   console.log(req.user);
    if(err) return res.send({code:500,
        message:'获取处理器品牌失败！'
    })

    if(ress){
        res.send({
            code:200,
            data:ress,
            message:'获取处理器品牌成功'
        })
    }

})
})

// 获取处理器型号
router.post('/chuliqixing',(req,res)=>{
    const sqlurlt = "select * from processor_model where brand_id=? and openid = ?";
    
  

db.query(sqlurlt,[req.body.id ,req.user.id],(err,ress)=>{
   
    if(err) return res.send({code:500,
        message:'获取处理器型号失败！'
    })

    if(ress){
        res.send({
            code:200,
            data:ress,
            message:'获取处理器型号成功'
        })
    }

})
})
// 获取箱体
router.post('/xiangti',(req,res)=>{
    const sqlurlt =
    "select * from box";
    

db.query(sqlurlt,(err,ress)=>{
   
    if(err) return res.send({code:500,
        message:'获取箱体失败！'
    })

    if(ress){
        res.send({
            code:200,
            data:ress,
            message:'获取箱体成功'
        })
    }

})
})

// 获取组装箱体
router.post('/zuxiangti',(req,res)=>{
    const sqlurlt = "select * from ssembling_box where brand_id=? and openid = ?";
      
    

db.query(sqlurlt,[req.body.id,req.user.id],(err,ress)=>{
   
    if(err) return res.send({code:500,
        message:'获取组装箱体失败！'
    })

    if(ress){
        res.send({
            code:200,
            data:ress,
            message:'获取组装箱体成功'
        })
    }

})
})

// 获取框架
router.post('/kuangjia',(req,res)=>{
    const sqlurlt =
        "select * from framework where brand_id=? and openid = ?";
   
    

db.query(sqlurlt,[req.body.id,req.user.id],(err,ress)=>{
   
    if(err) return res.send({code:500,
        message:'获取框架失败！'
    })

    if(ress){
        res.send({
            code:200,
            data:ress,
            message:'获取框架成功'
        })
    }

})
})
// 获取背条
router.post('/beitiao',(req,res)=>{
    const sqlurlt =
        "select * from dorsal_strip where openid = ?";
   
db.query(sqlurlt,req.user.id,(err,ress)=>{
   
    if(err) return res.send({code:500,
        message:'获取背条失败！'
    })

    if(ress){
        res.send({
            code:200,
            data:ress,
            message:'获取背条成功'
        })
    }

})
})

// 获取网线  
router.post('/wangxian',(req,res)=>{
    const sqlurlt =
    "select * from network where openid = ?";

   

db.query(sqlurlt,req.user.id,(err,ress)=>{
   
    if(err) return res.send({code:500,
        message:'获取网线失败！'
    })

    if(ress){
        res.send({
            code:200,
            data:ress,
            message:'获取网线成功'
        })
    }

})
})

// 获取税率
router.post('/shuilv',(req,res)=>{
    const sqlurlt =
        "select * from tax_rate where openid = ?";
   

db.query(sqlurlt,req.user.id,(err,ress)=>{
   
    if(err) return res.send({code:500,
        message:'获取税率失败！'
    })

    if(ress){
        res.send({
            code:200,
            data:ress,
            message:'获取税率成功'
        })
    }

})
})

// 获取排线

router.post('/Getpaixian',(req,res)=>{
    const sqlurlt =
    "select * from paixian where model=? and openid = ?";


db.query(sqlurlt,[req.body.model,req.user.id],(err,ress)=>{
   
    if(err) return res.send({code:500,
        message:'获取排线失败！'
    })

    if(ress){
        res.send({
            code:200,
            data:ress,
            message:'获取排线成功'
        })
    }

})
})

// 获取黑红线

router.post('/Gethiegong',(req,res)=>{
    const sqlurlt =
        "select * from hiehong where model=? and openid = ?";
      
     

     
db.query(sqlurlt,[req.body.model,req.user.id],(err,ress)=>{
   
    if(err) return res.send({code:500,
        message:'获取黑红线失败！'
    })

    if(ress){
        res.send({
            code:200,
            data:ress,
            message:'获取黑红线成功'
        })
    }

})
})


// 获取配电柜

router.post('/Getpeidiangui',(req,res)=>{
    const sqlurlt =
        "select * from peidian where model=? and openid = ?";
      
     

     
db.query(sqlurlt,[req.body.model,req.user.id],(err,ress)=>{
   
    if(err) return res.send({code:500,
        message:'获取配电柜失败！'
    })

    if(ress){
        res.send({
            code:200,
            data:ress,
            message:'获取配电柜成功'
        })
    }

})
})
//获取电脑
router.post('/Getdiannao',(req,res)=>{
    const sqlurlt =
        "select * from diannao where model=? and openid= ?";
      
     

     
db.query(sqlurlt,[req.body.model,req.user.id],(err,ress)=>{
   
    if(err) return res.send({code:500,
        message:'获取电脑失败！'
    })

    if(ress){
        res.send({
            code:200,
            data:ress,
            message:'获取电脑成功'
        })
    }

})
})
// 获取空调
router.post('/Getkongrtiao',(req,res)=>{
    const sqlurlt =
        "select * from kongtiao where model=? and openid = ?";
db.query(sqlurlt,[req.body.model,req.user.id],(err,ress)=>{
   
    if(err) return res.send({code:500,
        message:'获取空调失败！'
    })

    if(ress){
        res.send({
            code:200,
            data:ress,
            message:'获取空调成功'
        })
    }

})
})

// 获取安装
router.post('/Getanzhuang',(req,res)=>{
    const sqlurlt =
        "select * from anzhuang where model=? and openid = ?";
db.query(sqlurlt,[req.body.model,req.user.id],(err,ress)=>{
   
    if(err) return res.send({code:500,
        message:'获取空调失败！'
    })

    if(ress){
        res.send({
            code:200,
            data:ress,
            message:'获取空调成功'
        })
    }

})
})


// 提交excel数据
router.post('/gerisd',chuang)

// 创建获取聊天对象的接口
module.exports = router;
